/**
 * 网点活跃度
 * @date 20191205
 */
define(['plugins/echarts/echarts.min'], function (echarts) {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        table,
        cols = [[
            {field: 'ROW_ID', title: '序号', width: 60, fixed: 'left'}
            , {field: 'GWAK010', title: '销售渠道', width: 150, fixed: 'left'}
            , {field: 'NETCOUNT', title: '网点数', width: 80, search: 'default'}
            , {field: 'ACTIVECOUNT', title: '活跃数', width: 80, search: 'default'}
            , {field: 'REALVAL', title: '实际销售额', width: 80, filterDatas: app.datas_('sys_y_n')},
        ]],
        moduleName = '网点活跃度',
        where={},
        d = new Date(),
        editArea = ['800px', '400px']
    ;

    var Gwdc003 = app.Module.extend({
        init: function () {
            self = this;
            self.data = {
                list: [],
                GWAK010s: [],
                reals: [],
                dateRange:[],
                qd:'',
                startDate:d.getCurrentMonth()[0],//本月开始日期
                endDate:d.Format('yyyy-MM-dd')
            }
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'hyd',
                _searchs: [{
                    id: 'gwdc003', tableId: 'gwdc003_table', datas: _searchsArr.pushArray([
                        {field: 'GWDC016_1', width: 160, title: '渠道', search: 'default', searchOpts: {icon: true}},
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                },
                {
                    dom: 'hyd-info', tpl: 'hyd-info'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'hyd'),
                    data: self.data.list,
                    title: moduleName + '表',
                    id: 'hyd',
                    defaultToolbar: [],
                    height: 'full-400',
                    cols: cols,
                    page: true,
                    where: where,
                });

                var tipsIndex = null;

                //监听行双击事件
                table.on('rowDouble(hyd_table)', function (obj) {
                    self._openTplFull({
                        tag: 'hyd-info',
                        tagData: {
                            item: obj,
                            d:self.data
                        },
                        index: 1,
                        openOpts: {
                            title: '查看',
                            area:editArea,
                            maxmin: true
                        }
                    });
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'hyd_table', where: where});
        },
        _ready: function () {
            self.data.dateRange = [self.data.startDate,self.data.endDate]
            this._super({
                vue: [{
                    el: '#page',
                    data: self.data,
                    searchId: 'gwdc003',
                    watch:{
                      'dateRange':function (res) {
                          var startDate=  new Date(res[0]);
                          var startmonth=startDate.getMonth()+1;
                          var startday=startDate.getDate();
                          var endDate=  new Date(res[1]);
                          var endmonth=endDate.getMonth()+1;
                          var endday=endDate.getDate();
                          self.data.startDate =startDate.getFullYear()+'-'+(startmonth<10?'0'+startmonth:startmonth)+'-'+(startday<10?'0'+startday:startday);
                          self.data.endDate = endDate.getFullYear()+'-'+(endmonth<10?'0'+endmonth:endmonth)+'-'+(endday<10?'0'+endday:endday);
                          self.initData();
                      },
                      'qd':function (res) {
                          self.initData();
                      }
                    },
                    methods: {
                        handleIconClick: function (field) {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwae001',
                                        columnList: [{label: '销售渠道编号', prop: 'GWAE001', width: '150'},
                                            {label: '说明', prop: 'GWAEL003', width: '150'},
                                            {label: '助记码', prop: 'GWAEL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        self.data.qd = data.GWAE001
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择渠道信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                    }
                }]
            });

            self.initData()
        },
        initData: function () {
            app.ajax({
                xurl: '/bb/wdhyd/getList',
                xdata: self.data,
                loading:true,
                cb: function (res) {
                    if (res.ok) {
                        var data = res.data.data;
                        data.forEach(function (item, index) {
                            item.ROW_ID = index + 1;
                            self.data.reals.push(item.REALVAL);
                            self.data.GWAK010s.push(item.GWAK010)
                        });
                        self.data.list = data;
                        self.formChat();
                        self.initTable();
                    }
                }
            })
        },
        formChat: function () {
            var chartDiv = echarts.init(doc.getElementById('divChart'));

            var option = {
                legend: {
                    data: self.data.GWAK010s
                },
                grid: {
                    x: 35,
                    x2: 10,
                    y: 30,
                    y2: 25
                },
                // 提示框组件
                tooltip: {
                    trigger: 'axis', // 触发类型。坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用/none什么都不触发
                },
                toolbox: {
                    show: false,
                    feature: {
                        mark: {
                            show: true
                        },
                        dataView: {
                            show: true,
                            readOnly: false
                        },
                        magicType: {
                            show: true,
                            type: ['bar']
                        },
                        restore: {
                            show: true
                        },
                        saveAsImage: {
                            show: true
                        }
                    }
                },
                calculable: false,
                xAxis: [{
                    type: 'category',
                    data: self.data.GWAK010s
                }],
                yAxis: [{
                    type: 'value',
                    axisLabel: {
                        formatter: function (d) {
                            return d;
                        }
                    },
                    splitArea: {
                        show: true
                    }
                }],
                series: [{
                    name: '当天拜访',
                    type: 'bar',
                    data: self.data.reals,
                    label: {
                        normal: {
                            formatter: function (a) {
                                return a.data;
                            },
                            show: true,
                        }
                    },
                }]
            };
            chartDiv.setOption(option);
            self.data.GWAK010s.pushArray([],true);
            self.data.reals.pushArray([],true);
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwdc003();

});